package com.castle.fortress.admin.cms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.cms.dto.CmsRoleChannelDto;
import com.castle.fortress.admin.cms.entity.CmsRoleChannelEntity;
import com.castle.fortress.admin.cms.service.CmsRoleChannelService;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.utils.ExcelUtils;
import com.castle.fortress.common.entity.DynamicExcelEntity;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.utils.CommonUtil;
import com.castle.fortress.common.utils.ConvertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * cms栏目角色关联表 控制器
 *
 * @author castle
 * @since 2022-06-29
 */
@Api(tags="cms栏目角色关联表管理控制器")
@Controller
public class CmsRoleChannelController {
    @Autowired
    private CmsRoleChannelService cmsRoleChannelService;

    /**
     * cms栏目角色关联表的分页展示
     * @param cmsRoleChannelDto cms栏目角色关联表实体类
     * @param current 当前页
     * @param size  每页记录数
     * @return
     */
    @ApiOperation("cms栏目角色关联表分页展示")
    @GetMapping("/cms/cmsRoleChannel/page")
    @ResponseBody
    @RequiresPermissions("cms:cmsRoleChannel:pageList")
    public RespBody<IPage<CmsRoleChannelDto>> pageCmsRoleChannel(CmsRoleChannelDto cmsRoleChannelDto, @RequestParam(required = false) Integer current, @RequestParam(required = false)Integer size){
        Integer pageIndex= current==null? GlobalConstants.DEFAULT_PAGE_INDEX:current;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<CmsRoleChannelDto> page = new Page(pageIndex, pageSize);
        IPage<CmsRoleChannelDto> pages = cmsRoleChannelService.pageCmsRoleChannel(page, cmsRoleChannelDto);

        return RespBody.data(pages);
    }

    /**
     * cms栏目角色关联表的列表展示
     * @param cmsRoleChannelDto cms栏目角色关联表实体类
     * @return
     */
    @ApiOperation("cms栏目角色关联表列表展示")
    @GetMapping("/cms/cmsRoleChannel/list")
    @ResponseBody
    public RespBody<List<CmsRoleChannelDto>> listCmsRoleChannel(CmsRoleChannelDto cmsRoleChannelDto){
        List<CmsRoleChannelDto> list = cmsRoleChannelService.listCmsRoleChannel(cmsRoleChannelDto);
        return RespBody.data(list);
    }

    /**
     * cms栏目角色关联表保存
     * @param map cms栏目角色关联表实体类
     * @return
     */
    @ApiOperation("cms栏目角色关联表保存")
    @PostMapping("/cms/cmsRoleChannel/save")
    @ResponseBody
    @RequiresPermissions("cms:cmsRoleChannel:save")
    public RespBody<String> saveCmsRoleChannel(@RequestBody Map<String,Object> map){
        if(CommonUtil.verifyParamNull(map,map.get("roleId"),map.get("channelIds"))){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        try {
            Long roleId =Long.parseLong(map.get("roleId").toString());
            List<String> channelIds = (List<String>)map.get("channelIds");
            QueryWrapper<CmsRoleChannelEntity> delWrapper = new QueryWrapper<>();
            delWrapper.eq("role_id",roleId);
            cmsRoleChannelService.remove(delWrapper);
            List<CmsRoleChannelEntity> list = new ArrayList<>();
            for(String c:channelIds){
                CmsRoleChannelEntity e = new CmsRoleChannelEntity();
                e.setRoleId(roleId);
                e.setChannelId(Long.parseLong(c));
                list.add(e);
            }
            if(cmsRoleChannelService.saveBatch(list)){
                return RespBody.data("保存成功");
            }else{
                return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
            }
        } catch (Exception e) {
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * cms栏目角色关联表编辑
     * @param cmsRoleChannelDto cms栏目角色关联表实体类
     * @return
     */
    @ApiOperation("cms栏目角色关联表编辑")
    @PostMapping("/cms/cmsRoleChannel/edit")
    @ResponseBody
    @RequiresPermissions("cms:cmsRoleChannel:edit")
    public RespBody<String> updateCmsRoleChannel(@RequestBody CmsRoleChannelDto cmsRoleChannelDto){
        if(cmsRoleChannelDto == null || cmsRoleChannelDto.getId() == null || cmsRoleChannelDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        CmsRoleChannelEntity cmsRoleChannelEntity = ConvertUtil.transformObj(cmsRoleChannelDto,CmsRoleChannelEntity.class);
        if(cmsRoleChannelService.updateById(cmsRoleChannelEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * cms栏目角色关联表删除
     * @param id
     * @return
     */
    @ApiOperation("cms栏目角色关联表删除")
    @PostMapping("/cms/cmsRoleChannel/delete")
    @ResponseBody
    @RequiresPermissions("cms:cmsRoleChannel:delete")
    public RespBody<String> deleteCmsRoleChannel(@RequestParam Long id){
        if(id == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(cmsRoleChannelService.removeById(id)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }


    /**
     * cms栏目角色关联表批量删除
     * @param ids
     * @return
     */
    @ApiOperation("cms栏目角色关联表批量删除")
    @PostMapping("/cms/cmsRoleChannel/deleteBatch")
    @ResponseBody
    @RequiresPermissions("cms:cmsRoleChannel:deleteBatch")
    public RespBody<String> deleteCmsRoleChannelBatch(@RequestBody List<Long> ids){
        if(ids == null || ids.size()<1){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(cmsRoleChannelService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * cms栏目角色关联表详情
     * @param id cms栏目角色关联表id
     * @return
     */
    @ApiOperation("cms栏目角色关联表详情")
    @GetMapping("/cms/cmsRoleChannel/info")
    @ResponseBody
    @RequiresPermissions("cms:cmsRoleChannel:info")
    public RespBody<CmsRoleChannelDto> infoCmsRoleChannel(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        CmsRoleChannelEntity cmsRoleChannelEntity = cmsRoleChannelService.getById(id);
        CmsRoleChannelDto cmsRoleChannelDto = ConvertUtil.transformObj(cmsRoleChannelEntity,CmsRoleChannelDto.class);

        return RespBody.data(cmsRoleChannelDto);
    }

	/**
     * 动态表头导出 依据展示的字段导出对应报表
     * @param dynamicExcelEntity
     * @param response
     * @throws Exception
     */
	@PostMapping("/cms/cmsRoleChannel/exportDynamic")
	@ApiOperation("动态表头导出，依据展示的字段导出对应报表")
	public void exportDynamic(@RequestBody DynamicExcelEntity<CmsRoleChannelDto> dynamicExcelEntity, HttpServletResponse response) throws Exception {
		List<CmsRoleChannelDto> list = cmsRoleChannelService.listCmsRoleChannel(dynamicExcelEntity.getDto());
		//字典、枚举、接口、json常量等转义后的列表数据 根据实际情况初始化该对象，null为list数据直接导出
		List<List<Object>> dataList = null;
		/**
        * 根据实际情况初始化dataList,可参照 com.castle.fortress.admin.system.controller.TmpDemoController类中方法：exportDynamic
        */
		ExcelUtils.exportDynamic(response,dynamicExcelEntity.getFileName()+".xlsx",null,list,dynamicExcelEntity.getHeaderList(),dataList);
	}


}
